﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.GameService;
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 用户压分记录视图模型
    /// </summary>
    public class UserBettingRecordsViewModel : ShowListViewModelBase<BettingRecordExport, GameServiceClient>
    {
        public DateTime? beginTime = null;
        public DateTime? endTime = null;
        string keyword;
        ObservableCollection<FilterBoxModel> statusGroups = new ObservableCollection<FilterBoxModel>();

        public ObservableCollection<FilterBoxModel> StatusGroups
        {
            get { return statusGroups; }
            set
            {
                if (statusGroups == value) { return; }
                statusGroups = value;
                OnPropertyChanged("StatusGroups");
            }
        }

        public string Keyword
        {
            get { return keyword; }
            set
            {
                if (keyword == value) { return; }
                keyword = value == "" ? null : value;
                OnPropertyChanged("Keyword");
                Refresh(1);
            }
        }
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                    this.Refresh(1);
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                    this.Refresh(1);
                }
            }
        }


        public UserBettingRecordsViewModel()
            : base(PageCategory.游戏管理.ToString(),Page.用户压分记录.ToString())
        {
            this.client.GetBettingRecordsCompleted += 
                this.WrapEventHandler<GetBettingRecordsCompletedEventArgs>(GetBettingRecordsCompleted);
            StatusGroups.Add(new FilterBoxModel("全部", null, Refresh, true));
            StatusGroups.Add(new FilterBoxModel(BettingStatus.中奖.ToString(), BettingStatus.中奖, Refresh));
            StatusGroups.Add(new FilterBoxModel(BettingStatus.未中奖.ToString(), BettingStatus.未中奖, Refresh));
        }

        void GetBettingRecordsCompleted(object sender, GetBettingRecordsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }
 

        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            BettingStatus? sta = null;
            FilterBoxModel selected = statusGroups.First(x => x.Selected);
            if (selected != null)
            {
                if (selected.Value != null)
                    sta = (BettingStatus?)selected.Value;
            }
            client.GetBettingRecordsAsync(new GetBettingRecordsImport()
            {
                BeginTime = this.BeginTime,
                EndTime = this.EndTime,
                PageIndex = pageIndex, Keyword=this.keyword, Status=sta,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        protected override void Reset(object obj)
        {
            this.BeginTime = null;
            this.EndTime = null;
            this.PageIndex = 1;
            Refresh(null);
        }
    }
}
